﻿using DynamicApi.Utility.Attributes;
using Microsoft.AspNetCore.Mvc.ApplicationModels; 
using System.Reflection;

namespace DynamicApi.Utility.Provider;

public class ServiceActionRouteFactory : IActionRouteFactory
{
    public string CreateActionRouteModel(string areaName, string controllerName, ActionModel action)
    {
        var controllerType = action.ActionMethod.DeclaringType;
        var serviceAttribute = controllerType.GetCustomAttribute<ServiceAttribute>();

        var _controllerName = serviceAttribute.ServiceName == string.Empty 
            ? controllerName.Replace("Service", "") 
            : serviceAttribute.ServiceName.Replace("Service", "");

        return $"api/{_controllerName}/{action.ActionName.Replace("Async", "")}";
    }
}